package com.example.onlinemusic.controller;

import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.service.MusicService;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("music")
public class MusicController {

    @Autowired
    private MusicService musicService;

    @RequestMapping("/upload")
    public ResponseBodyMessage<Boolean> insertMusic (@RequestParam String singer,
                                                     @RequestParam("filename") MultipartFile file,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response) throws IOException {
        return musicService.uploadmusic(singer, file, request, response);
    }

    @RequestMapping("/get")
    public ResponseEntity<byte[]> get(String path) {
        return musicService.get(path);
    }

    // 删除单一歌曲
    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> deleteMusicById(@RequestParam String id) {
        return musicService.deleteMusicById(id);
    }

    /**
     * 批量删除
     * @param id [1,3,5,7,9]
     * @return
     */
    @RequestMapping("/deleteSel")
    public ResponseBodyMessage<Boolean> deleteMusic(@RequestParam("id[]") List<Integer> id) {
        return musicService.deleteMusic(id);
    }

    @RequestMapping("/findMusic")
    public ResponseBodyMessage<List<Music>> findMusic(@RequestParam(required = false) String musicName) {
        return musicService.findMusic(musicName);
    }
}
